package com.reptile.util;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.reptile.entity.TreeNode;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class TreeUtil {


    public static Map<String,Object> mapArray = new LinkedHashMap<String, Object>();
    public List<TreeNode> menuCommon;
    public List<Object> list = new ArrayList<Object>();

    public List<Object> treeMenu(List<TreeNode> menu){
        this.menuCommon = menu;
        for (TreeNode treeNode : menu) {
            Map<String,Object> mapArr = new LinkedHashMap<String, Object>();
            if(treeNode.getParentId().equals("0")){
                setTreeMap(mapArr,treeNode);
                list.add(mapArr);
            }
        }
        return list;
    }

    public List<?> menuChild(String id){
        List<Object> lists = new ArrayList<Object>();
        for(TreeNode a:menuCommon){
            Map<String,Object> childArray = new LinkedHashMap<String, Object>();
            if(a.getParentId() .equals(id)){
                setTreeMap(childArray,a);
                lists.add(childArray);
            }
        }
        return lists;
    }

    private void setTreeMap(Map<String,Object> mapArr,TreeNode treeNode){
        mapArr.put("id", treeNode.getId());
        mapArr.put("name", treeNode.getName());
        mapArr.put("parentId", treeNode.getParentId());
        List<?> childrens = menuChild(treeNode.getId());
        if(childrens.size()>0){
            mapArr.put("hasChild",true);
        }
        else{
            mapArr.put("hasChildren",false);
        }
        mapArr.put("childrens", menuChild(treeNode.getId()));
    }

    public static void main(String[] args){

        List<TreeNode> treeNodeList = new ArrayList<>();
        TreeNode treeNode1 = new TreeNode("1","0","首页");
        TreeNode treeNode2 = new TreeNode("2","0","订单");
        TreeNode treeNode3 = new TreeNode("3","1","预约");
        TreeNode treeNode4 = new TreeNode("4","2","捐献");
        TreeNode treeNode5 = new TreeNode("5","4","我的订单");
        TreeNode treeNode6 = new TreeNode("6","5","个人中心");
        TreeNode treeNode7 = new TreeNode("7","6","个人中心2");
        TreeNode treeNode8 = new TreeNode("8","99","个人中心3");
        treeNodeList.add(treeNode1);
        treeNodeList.add(treeNode6);
        treeNodeList.add(treeNode5);
        treeNodeList.add(treeNode3);
        treeNodeList.add(treeNode4);
        treeNodeList.add(treeNode2);
        treeNodeList.add(treeNode7);
        treeNodeList.add(treeNode8);

        TreeUtil treeUtil = new TreeUtil();

        Object result = JSON.toJSON(treeUtil.treeMenu(treeNodeList));


        System.out.print("******************反解析******************");
        String ss = "[{\"id\":\"1\",\"name\":\"首页\",\"parentId\":\"0\",\"hasChild\":true,\"childrens\":[{\"id\":\"3\",\"name\":\"预约\",\"parentId\":\"1\",\"hasChildren\":false,\"childrens\":[]}]},{\"id\":\"2\",\"name\":\"订单\",\"parentId\":\"0\",\"hasChild\":true,\"childrens\":[{\"id\":\"4\",\"name\":\"捐献\",\"parentId\":\"2\",\"hasChild\":true,\"childrens\":[{\"id\":\"5\",\"name\":\"我的订单\",\"parentId\":\"4\",\"hasChild\":true,\"childrens\":[{\"id\":\"6\",\"name\":\"个人中心\",\"parentId\":\"5\",\"hasChild\":true,\"childrens\":[{\"id\":\"7\",\"name\":\"个人中心2\",\"parentId\":\"6\",\"hasChildren\":false,\"childrens\":[]}]}]}]}]}]";
        String sss = result.toString();

        List<TreeNode> parent = new ArrayList<>();
        JSONArray jsonObject=  JSONObject.parseArray(sss);
        for (int i = 0; i < jsonObject.size(); i++) {
            TreeNode treeNode= JSON.toJavaObject((JSON) jsonObject.get(i),TreeNode.class);

            parent.add(treeNode);
        }
        System.out.println(JSON.toJSON(parent));
    }


    // 将jsonArray字符串转换成List集合
    public static List jsonToList(String json, Class beanClass) {
        if (null!=json) {
            //这里的JSONObject引入的是 com.alibaba.fastjson.JSONObject;
            return JSONObject.parseArray(json, beanClass);
        } else {
            return null;
        }
    }

}
